<template>
  <!-- 生产订单表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="生产订单编号" prop="produceOrderCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.produceOrderCode"
          placeholder="请输入生产订单编号"
          clearable
        />

        <span v-else>{{ formData.produceOrderCode }}</span>
      </el-form-item>
      <el-form-item label="公司编号" prop="organizationCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.organizationCode"
          placeholder="请输入公司编号"
          clearable
        />

        <span v-else>{{ formData.organizationCode }}</span>
      </el-form-item>
      <el-form-item label="工厂编号" prop="plantCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantCode"
          placeholder="请输入工厂编号"
          clearable
        />

        <span v-else>{{ formData.plantCode }}</span>
      </el-form-item>
      <el-form-item label="车间编号" prop="workshopCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workshopCode"
          placeholder="请输入车间编号"
          clearable
        />

        <span v-else>{{ formData.workshopCode }}</span>
      </el-form-item>
      <el-form-item label="工段编号" prop="workshopSectionCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workshopSectionCode"
          placeholder="请输入工段编号"
          clearable
        />

        <span v-else>{{ formData.workshopSectionCode }}</span>
      </el-form-item>
      <el-form-item label="工作中心编号" prop="workCenterCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterCode"
          placeholder="请输入工作中心编号"
          clearable
        />

        <span v-else>{{ formData.workCenterCode }}</span>
      </el-form-item>
      <el-form-item label="计划日期" prop="planDate">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planDate"
          placeholder="请输入计划日期"
          clearable
        />

        <span v-else>{{ formData.planDate }}</span>
      </el-form-item>
      <el-form-item label="计划班制id" prop="shiftSystemId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.shiftSystemId"
          placeholder="请输入计划班制id"
          clearable
        />

        <span v-else>{{ formData.shiftSystemId }}</span>
      </el-form-item>
      <el-form-item label="计划班次id" prop="shiftId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.shiftId"
          placeholder="请输入计划班次id"
          clearable
        />

        <span v-else>{{ formData.shiftId }}</span>
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialCode"
          placeholder="请输入物料编号"
          clearable
        />

        <span v-else>{{ formData.materialCode }}</span>
      </el-form-item>
      <el-form-item label="计划数量" prop="planQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planQuantity"
          placeholder="请输入计划数量"
          clearable
        />

        <span v-else>{{ formData.planQuantity }}</span>
      </el-form-item>
      <el-form-item label="计划批次" prop="planBatch">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planBatch"
          placeholder="请输入计划批次"
          clearable
        />

        <span v-else>{{ formData.planBatch }}</span>
      </el-form-item>
      <el-form-item label="已完成数量" prop="planFinishQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planFinishQuantity"
          placeholder="请输入已完成数量"
          clearable
        />

        <span v-else>{{ formData.planFinishQuantity }}</span>
      </el-form-item>
      <el-form-item label="数量单位" prop="quantityUnit">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.quantityUnit"
          placeholder="请输入数量单位"
          clearable
        />

        <span v-else>{{ formData.quantityUnit }}</span>
      </el-form-item>
      <el-form-item label="订单来源" prop="produceOrderSource">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.produceOrderSource"
          placeholder="请输入订单来源"
          clearable
        />

        <span v-else>{{ formData.produceOrderSource }}</span>
      </el-form-item>
      <el-form-item label="生产计划编号" prop="producePlanCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.producePlanCode"
          placeholder="请输入生产计划编号"
          clearable
        />

        <span v-else>{{ formData.producePlanCode }}</span>
      </el-form-item>
      <el-form-item label="父生产订单id" prop="parentProduceOrderId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.parentProduceOrderId"
          placeholder="请输入父生产订单id"
          clearable
        />

        <span v-else>{{ formData.parentProduceOrderId }}</span>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderState">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.orderState"
          placeholder="请输入订单状态"
          clearable
        />

        <span v-else>{{ formData.orderState }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESPRODUCEORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  produceOrderCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  organizationCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workshopCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workshopSectionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planDate: [],
  shiftSystemId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  shiftId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planQuantity: [],
  planBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planFinishQuantity: [],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  produceOrderSource: [],
  producePlanCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  parentProduceOrderId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  orderState: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  produceOrderCode: "",
  produceOrderType: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  workshopId: "",
  workshopCode: "",
  workshopSectionId: "",
  workshopSectionCode: "",
  workCenterId: "",
  workCenterCode: "",
  planDate: "",
  shiftSystemId: "",
  shiftId: "",
  routingAllocationId: "",
  materialId: "",
  materialCode: "",
  planQuantity: "",
  planBatch: "",
  planFinishQuantity: "",
  quantityUnit: "",
  produceOrderSource: "",
  producePlanId: "",
  producePlanCode: "",
  parentProduceOrderId: "",
  orderState: "",
  externalCode: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        produceOrderCode,
        produceOrderType,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workshopId,
        workshopCode,
        workshopSectionId,
        workshopSectionCode,
        workCenterId,
        workCenterCode,
        planDate,
        shiftSystemId,
        shiftId,
        routingAllocationId,
        materialId,
        materialCode,
        planQuantity,
        planBatch,
        planFinishQuantity,
        quantityUnit,
        produceOrderSource,
        producePlanId,
        producePlanCode,
        parentProduceOrderId,
        orderState,
        externalCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        produceOrderCode,
        produceOrderType,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workshopId,
        workshopCode,
        workshopSectionId,
        workshopSectionCode,
        workCenterId,
        workCenterCode,
        planDate,
        shiftSystemId,
        shiftId,
        routingAllocationId,
        materialId,
        materialCode,
        planQuantity,
        planBatch,
        planFinishQuantity,
        quantityUnit,
        produceOrderSource,
        producePlanId,
        producePlanCode,
        parentProduceOrderId,
        orderState,
        externalCode,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.produceOrderCode = "";
  formData.produceOrderType = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workshopId = "";
  formData.workshopCode = "";
  formData.workshopSectionId = "";
  formData.workshopSectionCode = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.planDate = "";
  formData.shiftSystemId = "";
  formData.shiftId = "";
  formData.routingAllocationId = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.planQuantity = "";
  formData.planBatch = "";
  formData.planFinishQuantity = "";
  formData.quantityUnit = "";
  formData.produceOrderSource = "";
  formData.producePlanId = "";
  formData.producePlanCode = "";
  formData.parentProduceOrderId = "";
  formData.orderState = "";
  formData.externalCode = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 生产订单保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesproduceorder/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
